﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ExtraClub.Domain.Entities;
using System.Web.Security;
using ExtraClub.Domain;
using SubSonic;
using System.Transactions;
using ExtraClub.Domain.Framework;
using LB.Generics;

namespace ExtraClub.Web.Forms.Service {
    public class club : BaseWS {

        #region ADD UPDATE DELETE

        protected override void Add(Dictionary<string, object> data) {
            Dictionary<string, object> userProfile = (Dictionary<string, object>)data["user-profile"];
            Dictionary<string, object> clubProfile = (Dictionary<string, object>)data["club-profile"];

            MembershipUser user = Membership.CreateUser(userProfile[AspnetUser.Columns.UserName.ToLower()].ToString(), userProfile[AspnetMembership.Columns.Password.ToLower()].ToString());
            if (user != null) {
                user.Email = userProfile[AspnetMembership.Columns.Email.ToLower()].ToString();
                Membership.UpdateUser(user);
                Roles.AddUserToRole(user.UserName, Constants.Role.CR);

                SaveClubProfile(user, clubProfile, userProfile, true);
            }
        }
        protected override void Update(Dictionary<string, object> data) {
            Dictionary<string, object> userProfile = (Dictionary<string, object>)data["user-profile"];
            Dictionary<string, object> clubProfile = (Dictionary<string, object>)data["club-profile"];

            MembershipUser user = Membership.GetUser(userProfile[AspnetUser.Columns.UserName.ToLower()].ToString());
            if (user != null) {
                user.Email = userProfile[AspnetMembership.Columns.Email.ToLower()].ToString();
                Membership.UpdateUser(user);

                user.ChangePassword(user.GetPassword(), userProfile[AspnetMembership.Columns.Password.ToLower()].ToString());
                Membership.UpdateUser(user);

                SaveClubProfile(user, clubProfile, userProfile, false);
            }
        }

        protected override void Delete(Dictionary<string, object> data) {
            if (!EntityId.HasValue) {
                throw new Exception("Not found entity");
            }

            Entity entity = Entity.FetchByID(EntityId);
            VwClubProfile.FetchByID(entity.Id);
            string userName = VwClubProfile.FetchByID(entity.Id).UserName;

            //CheckIfDeletionPermitied(entity);

            Entity.Delete(EntityId);
            Membership.DeleteUser(userName, true);
        }

        #endregion

        #region Routines
        protected void SaveClubProfile(MembershipUser user, Dictionary<string, object> clubProfile, Dictionary<string, object> userProfile, bool add) {
            Club club = null;
            Entity entity = null;
            AddressRepository address = null;
            AspnetUserProfile profile = null;

            /* User Profile */
            Guid createdById = (Guid)Membership.GetUser().ProviderUserKey;

            if (EntityId.HasValue) {
                profile = AspnetUserProfile.FetchByID((Guid)user.ProviderUserKey);
                club = Club.FetchByID(EntityId);
                entity = Entity.FetchByID(club.Id);
                address = AddressRepository.FetchByID(club.Id);
            } else {
                entity = new Entity();
                address = new AddressRepository();
                profile = new AspnetUserProfile();
                club = new Club();
            }

            /* User Profile */
            profile.FirstName = userProfile[AspnetUserProfile.Columns.FirstName.ToLower()].ToString();
            profile.FamilyName = userProfile[AspnetUserProfile.Columns.FamilyName.ToLower()].ToString();

            /* Club Profile */
            club.Name = clubProfile[Club.Columns.Name.ToLower()].ToString();
            club.ClubCategoryId = StringExt.IsInt32(clubProfile[Club.Columns.ClubCategoryId.ToLower()].ToString());
            club.IDNumber = StringExt.IsLong(clubProfile[Club.Columns.IDNumber.ToLower()]);

            /* Address */
            address.Email = clubProfile[AddressRepository.Columns.Email.ToLower()].ToString();
            address.CityRegion = clubProfile[AddressRepository.Columns.CityRegion.ToLower()].ToString();
            address.HouseNumber = clubProfile[AddressRepository.Columns.HouseNumber.ToLower()].ToString();
            address.Apartment = StringExt.IsByte(clubProfile[AddressRepository.Columns.Apartment.ToLower()]);
            address.Zip = StringExt.IsInt(clubProfile[AddressRepository.Columns.Zip.ToLower()]);

            object[] celular = (object[])clubProfile[AddressRepository.Columns.CellularPhone.ToLower()];
            address.CellularPhonePrefix = StringExt.IsByte(celular[0].ToString());
            address.CellularPhone = StringExt.IsLong(celular[1].ToString());

            object[] phone = (object[])clubProfile[AddressRepository.Columns.HomePhone.ToLower()];
            address.HomePhonePrefix = StringExt.IsByte(phone[0].ToString());
            address.HomePhone = StringExt.IsLong(phone[1].ToString());

            if (add) {
                entity.Created = DateTime.Now;
                entity.EntityType = "Club";
                entity.Save(createdById);

                profile.EntityId = entity.Id;
                profile.UserId = (Guid)user.ProviderUserKey;
                address.EntityId = entity.Id;
                club.Id = entity.Id;

            } else {
                entity.Modified = DateTime.Now;
                entity.Save(createdById);
            }

            profile.Save();
            club.Save();
            address.Save();
        }

        private void CheckIfDeletionPermitied(Club club) {
            //if (club.ClientRecords.Count > 0) {
            //    throw new Exception("Entity being deletied is not supplied");
            //}
        }
        #endregion
    }
}

